/**
 * PwdValidator.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年5月22日
 */
package com.pshare.framework.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.constant.SeaxConstants;
import com.pshare.framework.annotation.MoneyCheck;
import com.pshare.framework.annotation.PwdCheck;

/**
 * 金额验证
 */
public class MoneyValidator implements ConstraintValidator<MoneyCheck, CharSequence> {

	@Override
	public void initialize(MoneyCheck constraintAnnotation) {

	}

	@Override
	public boolean isValid(CharSequence object, ConstraintValidatorContext constraintContext) {
		if (StringUtils.isBlank(object))
			return true;
		
		String input = object.toString();
		boolean valid = true;
		if(!StringUtils.isBlank(input)){
			if (!isNumber(input)) {
				valid = false;
			}
		}
		
        if(!valid) {
            constraintContext.disableDefaultConstraintViolation();
            constraintContext.buildConstraintViolationWithTemplate(ReturnMsg.WRONG_FORMAT_MONEY).addConstraintViolation();
        }
        
        return valid;
	}
	
		//金额验证  
	   public  boolean isNumber(String str)   
	   {   
	       java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式  
	       java.util.regex.Matcher match=pattern.matcher(str);   
	       if(match.matches()==false)   
	       {   
	          return false;   
	       }   
	       else   
	       {   
	          return true;   
	       }   
	   }  

}
